package com.ymatou.query.seller.trading.api.parameter;

import java.util.List;
import java.util.Map;

import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.utils.DataUtil;

@SuppressWarnings("rawtypes")
public class Sp_GetAppSellerOrdersByOrderIds {
	private List<Integer> orderIds;
	private Integer sellerId = null;
	public List<Integer> getOrderIds() {
		return orderIds;
	}
	public void setOrderIds(List<Integer> orderIds) {
		this.orderIds = orderIds;
	}
	public Integer getSellerId() {
		return sellerId;
	}
	public void setSellerId(Integer sellerId) {
		this.sellerId = sellerId;
	}
	public boolean isDetail() {
		return detail;
	}
	public void setDetail(boolean detail) {
		this.detail = detail;
	}
	public List<Map> getOrders() {
		return orders;
	}
	public void setOrders(List<Map> orders) {
		this.orders = orders;
	}
	public List<Map> getOrderInfos() {
		return orderInfos;
	}
	public void setOrderInfos(List<Map> orderInfos) {
		this.orderInfos = orderInfos;
	}

	private boolean detail;
	

	public Sp_GetAppSellerOrdersByOrderIds execute() {
		if (DataUtil.isNullOrEmpty(orderIds)) {
			return this;
		}
		StringBuffer field1 = new StringBuffer();
		StringBuffer join1 = new StringBuffer();
		StringBuffer query1 = new StringBuffer();
		field1.append(" o.iMainOrderId,o.iOrderId,o.iUserId,o.iTradingStatus,o.sBuyerLoginId,o.sLeaveWord,o.fOrderPrice,o.fTotalPrice,o.bPaidInFull,o.iRiskVerifiedStatus,n.sContent as sellerNote");
		field1.append(" ,o.bShangouOrder");
		field1.append(" ,p.fAmount as postpayAmount,p.fUseGiftAmount as postpayUseGiftAmount");
		field1.append(" ,o.dAddTime,o.dApplyPostPayTime,o.dCancelTime,o.dConfirmedTime,o.dDispathTime,o.dPaidTime,o.dPostPaidTime");
		field1.append(" ,o.fFreight,o.fOrderDiscount,o.fDiscount,o.fUseGiftAmount");
		field1.append(" ,o.sCouponCode,o.CouponValue,o.iCouponChannel,o.sYmtCouponCode,o.fYmtCouponAmount,o.sSellerCouponCode,o.fSellerCouponAmount");
		field1.append(" ,o.sReceivePerson,o.sPhone,o.sPostCode,o.sAddress");
		field1.append(" ,e.bIsNeedUploadIdCard,e.bHaveUploadedIdCard,o.fSellerPromotionAmount");
		field1.append(" ,e.bNewSellerOrder,e.bNewCustomerOrder");
		field1.append(" ,case when bl.iSellerId is null then 0 else 1 end as buyerInBlacklist");
		if(detail){
			field1.append(" , (select top 1 sReason from Ymt_OrderReason(nolock) where iOrderId = o.iOrderId) as sCancelReason");
		}else{
			field1.append(" , null as sCancelReason");
		}
		join1.append(" left join  Ymt_OrderExt(nolock) e on o.iOrderId = e.iOrderId");
		join1.append(" left join Ymt_O_OrderNote(nolock) n on o.iOrderId = n.iOrderId and o.iBuyerId = n.iUserId");
		join1.append(" left join Ymt_BlackListForSeller(nolock) bl on o.iBuyerId = bl.iSellerId and o.iUserId = bl.iBuyerId and bl.iAction = 0");
		join1.append(" outer apply (select top 1 fAmount,fUseGiftAmount from Ymt_OrderPostPay(nolock) where iOrderId = o.iOrderId and (iAction = 0 or iAction = 1) order by iAction desc) p");
		query1.append(" o.iOrderId in " + DataUtil.toInOfSql(orderIds));
		query1.append(" order by o.iOrderId desc");
		orders = new YmtOrdersWapper().selectOrders2(field1.toString(),
				join1.toString(), query1.toString());

		StringBuffer field2 = new StringBuffer();
		StringBuffer join2 = new StringBuffer();
		StringBuffer query2 = new StringBuffer();
		field2.append(" oi.iOrderId,oi.sPictureUrl,oi.sPropertyInfo,oi.fThirdPartyDiscount,oi.sCatalogId,oi.iCatalogType,oi.sProductId,oi.fProductPrice,oi.fProductOriginalPrice,oi.fOriginalPrice,oi.sTitle,oi.iAmount,oi.iPriceType,oi.iCatalogStatus,oi.iProductRefundChannel,oi.iProductRefundStatus,oi.bPspProduct ");
		field2.append(" ,oi.fYmtCouponAmount,oi.fSellerCouponAmount,oi.fDiscount,oi.fFreight");
		field2.append(" ,oi.iTariffType,oi.bFreightFree,oi.bSupportRtnWithoutReason");
		if(detail){
			field2.append(" , (select top 1 RefundBillNo from ymt_refundproduct(nolock) where OrderInfoId = oi.sOrderInfoId) as SalesRefundCode");
			field2.append(" , (select top 1 b.RefundAmount from ymt_refundproduct(nolock) p join Ymt_RefundBill(nolock) b on p.RefundBillNo = b.RefundBillNo where p.OrderInfoId = oi.sOrderInfoId) as SalesRefundAmount");
		}else{
			field2.append(", null as SalesRefundCode");
			field2.append(", null as SalesRefundAmount");
		}
		field2.append(" , oi.[fSellerPromotionAmount] , s.[PromotionId] , s.[PromotionType] , s.[PromotionName] , s.[MatchCondition] , s.[PromotionValue]");
		field2.append(" , s.ReducedAmount");
		join2.append(" left join [dbo].[Ymt_SellerPromotion] as s with(nolock) on oi.[iOrderId] = s.[OrderId] and oi.[sOrderInfoId] = s.[OrderInfoId]");
		query2.append(" oi.[iOrderId] in " + DataUtil.toInOfSql(orderIds));
		orderInfos = new DefaultWapper().selectOrderInfo(field2.toString(),
				join2.toString(), query2.toString());
		return this;
	}

	private List<Map> orders;
	private List<Map> orderInfos;


}
